package com.nanhu.test.scroll;

import java.util.ArrayList;
import com.nanhu.test.R;
import com.nanhu.test.util.SlideMenuUtil;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScrollViewMenuActivity extends Activity{
	private HorizontalScrollView horizontalScrollView = null;
	
	private ImageView cursor;// 动画图片
	
	private int bmpW;// 动画图片宽度
	
	private int offset = 0;// 动画图片偏移量
	
	private LinearLayout titleContainer;
	
	static private int currentIndex = 0;
	
	ArrayList<View> pageViews;
	
	private ViewPager viewPager;
	
	static private int startIndex = 0;
	
	static private int endIndex;
	
	private final int blockWidth = 80; 
	
	private boolean isOrientationSwitch = false;
	
	static private int orientation = 1;
	
	private Handler handler;
	
    /** Called when the activity is first created. */
	private String[] menus = {SlideMenuUtil.ITEM_MOBILE,SlideMenuUtil.ITEM_WEB,
								SlideMenuUtil.ITEM_CLOUD,SlideMenuUtil.ITEM_DATABASE,
								SlideMenuUtil.ITEM_EMBED,SlideMenuUtil.ITEM_SERVER,
								SlideMenuUtil.ITEM_DOTNET,SlideMenuUtil.ITEM_JAVA,
								SlideMenuUtil.ITEM_SAFE,SlideMenuUtil.ITEM_DOMAIN,
								SlideMenuUtil.ITEM_RESEASRCH,SlideMenuUtil.ITEM_MANAGE};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.menu);

        handler = new Handler();
        
        int newOrientation = this.getResources().getConfiguration().orientation;
        if(orientation > 0 && orientation != newOrientation){
        	isOrientationSwitch = true;
        	orientation = newOrientation;
        }
        else{
        	isOrientationSwitch = false;
        }
        orientation = this.getResources().getConfiguration().orientation;
        
        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizonMenu);
        titleContainer = (LinearLayout)findViewById(R.id.titleContainer);
        setSlideMenu();
        InitImageView();
        
        DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = (int)(dm.widthPixels);// 获取分辨率宽度
        endIndex = screenW / blockWidth + startIndex - 1;
        
        LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.item05, null));
        pageViews.add(inflater.inflate(R.layout.item06, null));
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));
        pageViews.add(inflater.inflate(R.layout.item05, null));
        pageViews.add(inflater.inflate(R.layout.item06, null));
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));
        
        viewPager = (ViewPager)findViewById(R.id.guidePagers1);
        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
        viewPager.setCurrentItem(0);
    }
    
    public void goTo(int toIndex){
    	if(toIndex < 0 || toIndex > menus.length - 1)
    		return;
    	
    	int scrollIndex = 0;
    	if(toIndex < startIndex){
    		scrollIndex = toIndex - startIndex;
    		startIndex--;
    		endIndex--;
    	}else if(toIndex > endIndex){
    		scrollIndex = toIndex - endIndex;
    		startIndex++;
    		endIndex++;
    	}
    	if(isOrientationSwitch){
    		isOrientationSwitch = false;
    		scrollIndex = startIndex;
    		final int index = scrollIndex;
    		handler.postDelayed(new Runnable(){

				@Override
				public void run() {
					horizontalScrollView.smoothScrollTo(index * blockWidth, 0);
				}
    			
    		}, 50);
    	}else{
    		horizontalScrollView.smoothScrollBy(scrollIndex * blockWidth, 0);
    	}
    	
    	bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 获取图片宽度
    	
    	int fromX = currentIndex * blockWidth;
    	int toX = toIndex * blockWidth;
    	currentIndex = toIndex;
    	
    	Animation animation = new TranslateAnimation(fromX, toX, 0, 0);
    	animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		cursor.startAnimation(animation);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    }
    
    /**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		Log.i("MainActiviy", "手机屏幕分辨率：" + dm.widthPixels + " x " + dm.heightPixels);
		Log.i("MainActiviy", "手机屏幕密度：" + dm.density);
		
		//horizontalScrollView.computeHorizontalScrollRange();
		
		int screenW = (int)(dm.widthPixels);// 获取分辨率宽度
		offset = (blockWidth - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}
    
    private void setSlideMenu(){
    	// ��TextView��LinearLayout
		LinearLayout menuLinerLayout = (LinearLayout) findViewById(R.id.linearLayoutMenu);
		menuLinerLayout.setOrientation(LinearLayout.HORIZONTAL);
		// ��������
		LinearLayout.LayoutParams menuLinerLayoutParames = new LinearLayout.LayoutParams(blockWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		menuLinerLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;
		
		// ���TextView�ؼ�
    	for(int i = 0;i < menus.length;i++){
    		TextView tvMenu = new TextView(this);
			//tvMenu.setLayoutParams(new LayoutParams(30,30));
			tvMenu.setPadding(0, 14, 0, 10);
			tvMenu.setText(menus[i]);
			tvMenu.setTextColor(Color.WHITE);
			tvMenu.setGravity(Gravity.CENTER_HORIZONTAL);
			menuLinerLayout.addView(tvMenu,menuLinerLayoutParames);
        }
    }
    
    class GuidePageAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager)container).removeView(pageViews.get(position));
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}
		
		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
			super.restoreState(state, loader);
		}
		
		@Override
		public Parcelable saveState() {
			return super.saveState();
		}
		
		@Override
		public void startUpdate(View container) {
			super.startUpdate(container);
		}
		
		@Override
		public void finishUpdate(View container) {
			super.finishUpdate(container);
		}
	}
	
	class GuidePageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			goTo(arg0);
		}
		
	}
}
